package io.nanfeng.oscbot.service

import com.elbekD.bot.Bot
import com.elbekD.bot.feature.chain.chain
import com.elbekD.bot.feature.chain.jumpTo
import com.elbekD.bot.feature.chain.jumpToAndFire
import io.nanfeng.oscbot.property.BotProperty
import io.nanfeng.oscbot.property.ProxyProperty
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.stereotype.Service

@Service
class BotService(private val botProperty: BotProperty, private val proxyProperty: ProxyProperty):CommandLineRunner {


    override fun run(vararg args: String?) {
        val bot = Bot.createPolling(botProperty.botName, botProperty.botToken){
            proxyHost=proxyProperty.proxyHost
            proxyPort=proxyProperty.proxyPort
            proxyType=proxyProperty.proxyType
        }

        bot.chain("/start") { msg -> bot.sendMessage(msg.chat.id, "Hi! What is your name?") }
                .then { msg -> bot.sendMessage(msg.chat.id, "Nice to meet you, ${msg.text}! Send something to me") }
                .then { msg -> bot.sendMessage(msg.chat.id, "Fine! See you soon") }
                .build()

        bot.onAnyUpdate {
            println(it.toString())
        }

        bot.chain(
                label = "location_chain",
                predicate = { msg -> msg.location != null },
                action = { msg ->
                    bot.sendMessage(
                            msg.chat.id,
                            "Fine, u've sent me a location. Is this where you want to order a taxi?(yes|no)"
                    )
                })
                .then("answer_choice") { msg ->
                    when (msg.text) {
                        "yes" -> bot.jumpToAndFire("order_taxi", msg)
                        "no" -> bot.jumpToAndFire("cancel_ordering", msg)
                        else -> {
                            bot.sendMessage(msg.chat.id, "Oops, I don't understand you. Just answer yes or no?")
                            bot.jumpTo("answer_choice", msg)
                        }
                    }
                }
                .then("order_taxi", isTerminal = true) { msg ->
                    bot.sendMessage(msg.chat.id, "Fine! Taxi is coming")
                }
                .then("cancel_ordering", isTerminal = true) { msg ->
                    bot.sendMessage(msg.chat.id, "Ok! See you next time")
                }
                .build()

        bot.start()
    }
}